package com.office.anywher.email;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.office.anywher.beans.UserInfo;
import com.office.anywher.execeptions.OAException;
import com.office.anywher.https.HttpClientService;
import com.office.anywher.poly.list.Common;
import com.office.anywher.poly.list.IDataReceiveForList;
import com.wenxy.common.IConst;

public class ListAddress extends Common implements IDataReceiveForList{
	private int mType ;
	public ListAddress(int type){
		mType = type;
	}
	@Override
	public List<HashMap<String, Object>> receive(HttpClientService aService,
			int start, int rowNum) throws Exception {
		List<HashMap<String, Object>> rtnList = new ArrayList<HashMap<String, Object>>();
		UserInfo aUserInfo = aService.getUserInfo();
		JSONArray aJsonArray = aService.getAddressList(IConst.Http.Address.URL,aUserInfo.getUserOrganId(), mType ,start, rowNum);
		if(aJsonArray==null || aJsonArray.length()<=0){throw new OAException(OAException.OA_EXECEPTION_DEF[3]);}
		for(int i = 0;i<aJsonArray.length();i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			JSONObject aJsoObj = null;
			try {
				aJsoObj = aJsonArray.getJSONObject(i);
				if(aJsoObj == null)continue;
				if(mType == IConst.Http.Address.PERSONAL.TYPE){
					map.put(IConst.Http.Address.PERSONAL.OUT.SUB_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Address.PERSONAL.OUT.SUB_NAME)?"":aJsoObj.getString(IConst.Http.Address.PERSONAL.OUT.SUB_NAME));
					map.put(IConst.Http.Address.PERSONAL.OUT.MOBILE,isNullInJsonObject(aJsoObj,IConst.Http.Address.PERSONAL.OUT.MOBILE)?"":aJsoObj.getString(IConst.Http.Address.PERSONAL.OUT.MOBILE));

				}else if(mType == IConst.Http.Address.PUBLIC.TYPE){
					String CONTACT_PERSON = isNullInJsonObject(aJsoObj,IConst.Http.Address.PUBLIC.OUT.CONTACT_PERSON)?"":aJsoObj.getString(IConst.Http.Address.PUBLIC.OUT.CONTACT_PERSON);
					CONTACT_PERSON = CONTACT_PERSON+"["+(isNullInJsonObject(aJsoObj,IConst.Http.Address.PUBLIC.OUT.COP_NAME)?"":aJsoObj.getString(IConst.Http.Address.PUBLIC.OUT.COP_NAME))+"]";
					map.put(IConst.Http.Address.PUBLIC.OUT.CONTACT_PERSON,CONTACT_PERSON);
					map.put(IConst.Http.Address.PUBLIC.OUT.CONTACT_PHONE,isNullInJsonObject(aJsoObj,IConst.Http.Address.PUBLIC.OUT.CONTACT_PHONE)?"":aJsoObj.getString(IConst.Http.Address.PUBLIC.OUT.CONTACT_PHONE));
					map.put(IConst.Http.Address.PUBLIC.OUT.COP_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Address.PUBLIC.OUT.COP_NAME)?"":aJsoObj.getString(IConst.Http.Address.PUBLIC.OUT.COP_NAME));
					map.put(IConst.Http.Address.PUBLIC.OUT.MOBILE,isNullInJsonObject(aJsoObj,IConst.Http.Address.PUBLIC.OUT.MOBILE)?"":aJsoObj.getString(IConst.Http.Address.PUBLIC.OUT.MOBILE));
				}
				rtnList.add(map);
			} catch (JSONException e) {
				e.printStackTrace();
				throw new OAException(OAException.OA_EXECEPTION_DEF[2]);
			}
		}
		return rtnList;
	}

}
